﻿using B.S.Ingredients.APIRead.Application.Command.Products;
using B.S.Ingredients.Domain;
using B.S.Ingredients.ErrorCode;
using B.S.Ingredients.Infartructrue.Interfaces;
using MediatR;

namespace B.S.Ingredients.APIRead.Application.CommandHandler.ProductHandler
{
    public class ProductDelCommandHandler : IRequestHandler<ProductDelCommand, APIResult<int>>
    {
        public ProductDelCommandHandler(IBaseRepository<Product> prodicrepository)
        {
            Prodicrepository = prodicrepository;
        }

        public IBaseRepository<Product> Prodicrepository { get; }

        public async Task<APIResult<int>> Handle(ProductDelCommand request, CancellationToken cancellationToken)
        {
          List<int> ids=request.Id.Split(',').Select(a=>Convert.ToInt32(a)).ToList();

            var listDto= Prodicrepository.GetAll().Where(a => ids.Contains(a.ProductId)).ToList();

            if(listDto.Count>1)
            {
                foreach(var item in listDto)
                {
                    item.DelName="管理员";
                    item.DelTime=DateTime.Now;
                    item.isDel=true;
                }
              await  Prodicrepository.UpdateAsync(listDto);
            }
            else
            {
                listDto[0].DelName = "管理员";
                listDto[0].DelTime = DateTime.Now;
                listDto[0].isDel = true;
                await Prodicrepository.UpdateAsync(listDto);
            }

            return new APIResult<int>
            {
                Code = ResultCode.success,
                Mag = "删除成功",
                Data = 0
            };
        }

    

    }
}
